package de.tabuma.formcreator.formstructur.logik;

import de.tabuma.formcreator.logik.*;
import de.tabuma.formcreator.entity.*;
import de.tabuma.soap.entity.wsdl.*;
import de.tabuma.soap.entity.xsd.*;

import javax.xml.namespace.QName;
import java.util.*;

/**
  * @author $Author: $
  * @version $Revision: $
  * @since $Date: $
 */
public class FormStructurWithAttributeFactory {
    public static FormStructur createFormularStruktur(TDefinitions wsdl, XsAttribute xsAttribute) {
        String attributeName = xsAttribute.getName();
        if(attributeName==null) {
            attributeName = "";
        }
        
        String attributeType = xsAttribute.getType();
        if(attributeType==null) {
            attributeType = "";
        }
        
        boolean array = false;
        String arrayType = "";
        
        /*
         TODO
         Set<QName> mySet = xsAttribute.getOtherAttributes().keySet();
        Iterator<QName> myIt = mySet.iterator();
        while(myIt.hasNext()) {
            QName myQName = myIt.next();
            String value = xsAttribute.getOtherAttributes().get(myQName);
            if(value!=null) {
                if(value.endsWith("[]")) {
                    int index1 = value.indexOf(':');
                    arrayType = value.substring(index1+1,value.length()-2);//ohne [] am Ende
                    array = true;
                }
            }
        }*/
        FormStructur formularStruktur = new FormStructur(attributeName, attributeType, 1, 1, true, false, false, false);
        if(array) {
            XsElement xsElement = new XsElement();
            xsElement.setName("item");
            xsElement.setType(arrayType);
            formularStruktur = FormStructurWithComplexTypeFactory.createFormularStruktur2(wsdl, xsElement, 0);
        }
        
        

        return formularStruktur;
    }
    public static List<FormStructur> createFormularStruktur(TDefinitions wsdl, XsAttributeGroup xsAttributeGroup)  {
        ArrayList<FormStructur> formularStruktur = new ArrayList<FormStructur>();
        for(int i=0;i<xsAttributeGroup.getXsAttributeOrXsAttributeGroup().size();i++) {
            Object objekt = xsAttributeGroup.getXsAttributeOrXsAttributeGroup().get(i);
            if(XsAttribute.class.isInstance(objekt)) {
                XsAttribute xsAttribute = (XsAttribute) objekt;
                FormStructur formularStruktur1 = createFormularStruktur(wsdl, xsAttribute);
                if(formularStruktur1!=null) {
                    formularStruktur.add(formularStruktur1);
                }
            }
            else if(XsAttributeGroup.class.isInstance(objekt)) {
                XsAttributeGroup xsAttributeGroup1 = (XsAttributeGroup) objekt;
                List<FormStructur> formularStruktur1 = createFormularStruktur(wsdl, xsAttributeGroup1);
                if(formularStruktur1!=null) {
                    formularStruktur.addAll(formularStruktur1);
                }

            }
        }
        return formularStruktur;
    }
}
